<?php

namespace app\web\controller;

use app\common\controller\Frontend;
use app\common\model\goods\GoodsHouseStock;
use think\Db;

class Goods extends Frontend
{

    protected $noNeedLogin = '';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function addcart(){
        $goods_id = input('goods_id/d',0);
        $storehouse_id = input('storehouse_id/d',0);
        $row = Db::name('sales_cart')->where(array(
            'goods_id'=>$goods_id,
            'admin_id'=>session('admin')['id'],
            'sotre_house_id'=>$storehouse_id,
        ))->find();
        if($row){
           $res = Db::name('sales_cart')->where('id', $row['id'])->setInc('num');
        }else{
            $data = array(
                'goods_id'=>$goods_id,
                'admin_id'=>session('admin')['id'],
                'sotre_house_id'=>$storehouse_id,
                'createtime'=>time(),
                'num'=>1,
            );
            $res = Db::name('sales_cart')->insert($data);
        }
       if($res){
           $this->success('添加成功');
       }else{
           $this->error('添加失败');
       }
    }
    public function getcartcount(){
        $storehouse_id = input('storehouse_id/d',0);
        if(empty($storehouse_id)){
            if(empty(session('admin')['storehouse_id'])){
                $hrow = Db::name('storehouse')->where(array('is_default'=>1))->find();
                $storehouse_id = $hrow['id'];
            }else{
                $storehouse_id = session('admin')['storehouse_id'];
            }
        }
        $num = Db::name('sales_cart')->where(array(
            'admin_id'=>session('admin')['id'],
            'sotre_house_id'=>$storehouse_id,
        ))->sum('num');
        $this->success('','',$num);
    }
    public function cart(){
        //获取默认仓库或者所属仓库
        if(session('admin')['storehouse_id'] >0){
            $hrow = Db::name('storehouse')->where(array('id'=>session('admin')['storehouse_id']))->find();
        }else{
            $hrow = Db::name('storehouse')->where(array('is_default'=>1))->find();
        }
        $list = Db::name('sales_cart')
            ->field('c.id,c.num,g.title,g.specs,b.name,g.number')
            ->where(array(
                'admin_id'=>session('admin')['id'],
                'sotre_house_id'=>$hrow['id'],
            ))
            ->alias('c')
            ->join('goods g','c.goods_id = g.id','LEFT')
            ->join('supplier_brands b','g.brands_id = b.id','LEFT')
            ->select();
        $numbers = [];
        foreach ($list as &$v){
            $numbers[] = $v['number'];
            $house = new GoodsHouseStock();
            $v['stock'] = $house->getstock($v['id'],$hrow['id']);
        }
        $prices = $this->getprice(implode(',',$numbers),$hrow['code']);
        foreach ($list as &$v){
            $v['price'] = 0;
        }
        $this->assign('list',$list);
        $this->assign('purl','cart');
        return $this->view->fetch();
    }
//    protected function getprice($numbers,$code){
//        $url =  config('site.syncurl').'/api/sync/batchprice';
//        $json = curl_post($url,array(
//            'numbers'=>$numbers,
//            'code'=>$code,
//            'appkey'=>config('site.appkey'),
//        ));
//        $json = json_decode($json,true);
//        if($json['code'] == 9){
//            return [];
//        }
//        if(!empty($json) && $json['code']  == 1){
//            return $json['data'];
//        }else{
//            return [];
//        }
//    }
    public function updatecart(){
        $cart_id = input('cart_id/d',0);
        $num = input('num/d',0);
        $res = Db::name('sales_cart')->where(array('id'=>$cart_id))->update(array('num'=>$num));
        if($res){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }
    public function updatecarts(){
        $goods_id = input('goods_id/d',0);
        $num = input('num/d',0);
        $res = Db::name('sales_cart')->where(array('goods_id'=>$goods_id,'admin_id'=>session('admin')['id']))->update(array('num'=>$num));
        if($res){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }
    public function del(){
        $cart_id = input('cart_id','');
        $cart_ids = explode('-',$cart_id);
        $res = Db::name('sales_cart')->where(array('id'=>array('in',$cart_ids)))->delete();
        if($res){
            $this->success('删除成功','',$cart_id);
        }else{
            $this->error('删除失败');
        }
    }
}
